var xx = [];
var xz = [];
var app = getApp();
var clickId = "";
var count = "";
Page({
  data: {
    jiesuan:true,
    deted:true,
    allSelect:"circle",  //全选按键初始为空
    num:0,   //结算总数
    count:0   //合计价格
  },
  // 弹出是否取消页面
  modalTap: function (e) {
    this.setData({
      modalHidden: false
    })
  },
  modalChange: function (e) {
    this.setData({
      modalHidden: true
    })
  },
  modalTap2: function (e) {
    this.setData({
      modalHidden2: false
    })
  },
  modalChange2: function (e) {
    this.setData({
      modalHidden2: true
    })
  },

  // 弹出蛋糕详情页样式选择的JS
  tabClick: function (res) {
    var danjia = res.currentTarget.dataset.danjia;
    let p = res.currentTarget.dataset.id.split("_")[0];  //截取下标前一个  大循环的下标
    let c = res.currentTarget.dataset.id.split("_")[1];  //截取下标后一个  内循环样式的下标
    let list = this.data.listclass;   // 获取数据库中的单个蛋糕详情
    for (let j = 0; j < list[p].children.length; j++) {  // 遍历每一个样式大框下的子元素
      list[p].children[j].color = "false";    // 点击时使每一个元素颜色不变 
      list[p].children[c].color = "true";     // 只有当前点击的元素选中变色
    }
    
    var z1 = "";   // 建一个空的元素集合，为了下面放置选中的属性
    var ides = "";  // 同上
    var zongji = 0;
    for (let i = 0; i < list.length; i++) {  //  循环遍历外层，获取每一个外层下标值
      // console.log(list[i].children);  //打印出所有子元素
      
      for (let j = 0; j < list[i].children.length; j++) {  // 获取内层循环的数值，判断color为true时获值
        if (list[i].children[j].color == "true") {
          // console.log("就是这个" + list[i].children[j].id);
          //console.log(list[i].children[j]);
          z1 += list[i].children[j].value + " ";  //规格的名字串联
          ides += list[i].children[j].id + "-";   //规格的ID串联
          zongji += parseFloat(list[i].children[j].float_price);
        }
      }
    }
    var zongjias = parseFloat(zongji) + parseFloat(danjia);
   
    this.setData({
      listclass: list,   // 从数据库中Listclass拿值
      price: zongjias.toFixed(2),
      clickId: z1,       // 选中的规格名字显示在弹框的上方规格展示
      ides: ides,        //将选中的规格值串联出来
    })
  },

  // 点击确定获取弹出选项的值并回填
    onChangeShowState2: function (e) {
    var xuhao = e.currentTarget.dataset.xuhao;        // 点击弹出框的确定时            data-xu hao="{{xuh ao}}"
    var id = e.currentTarget.dataset.id;              // 这个是商品的ID
    var guige_id = e.currentTarget.dataset.name;      // 获取选中的规格的ID
    var danjia = e.currentTarget.dataset.danjia;
    var guige_name = e.currentTarget.dataset.guige;   //获取选中的规格的名字
    var that = this;
    
    wx.request({    //  向后台传递数值
      url: 'https://www.cakeyouxuan.com/small/cart/guige_edit',
      data: {
        id:id,           //递交选中的商品ID
        guige:guige_id,  //递交选中的规格ID
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
      },
    })
    var newList = that.data.list   // 当前购物车的所有商品的ID
    newList[xuhao].guige_info = guige_name      // 获取当前选中商品的data-xuhao的对应的规格名
    newList[xuhao].price = danjia

    that.setData({
      list: newList,   // newlist是从数据库中获取的商品所有信息
      showView: (!that.data.showView)   //弹出的选择规格的页面
    })
  },
  //点击弹出页面的右上角的X 使页面关闭
  clocks:function(){    
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  // 点击单个商品的编辑键使单个商品变成编辑模式
  onChangeShowState: function (e) {
    var carid = e.currentTarget.dataset.id;   // 获取点击商品的ID值
    var that = this;
    var list_ = this.data.list;     // 当前购物车中的所有商品
    var index_ = e.currentTarget.dataset.ind;   //当前点击的商品栏的下标
    // console.log(list_[index_]); console.log('20182018')
    if (list_[index_].show == true) {
      for (let i = 0; i < list_.length; i++) {
        // list_[index_].show = false;
        list_[index_].xs = 1;
      }
    } else {
      for (let i = 0; i < list_.length; i++) {
        list_[index_].show = true;
        list_[index_].xs = 0;
      }
    }
    var ns;
    for (let i = 0; i < list_.length; i++) {
      if (list_[i].xs == 0) {
        ns = 0;
      }
    }
    that.setData({
      ns: ns,
      list: list_,
      xuhao: index_,
    })
  },



  Todet: function (e) {
    var that = this;
    var ids = e.currentTarget.dataset.xuanz;  //选中的属性
    var uid = app.whereId.uid;
    var newCount = 0;
    var allNum = 0
    if (ids.length == 0) {
      wx.showToast({
        title: '未选中删除对象',
        duration: 2000
      });
    } else {
      wx.showModal({
        title: '提示',
        content: '确认取消该订单？',
        success: function (res) {
          wx.request({
            url: 'https://www.cakeyouxuan.com/small/cart/del',
            data: {
              ids: ids,
              uid: uid,
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"  //post
            },
            
            success: function (res) {
              // console.log(count);console.log('1223456')
              that.setData({
                list: res.data.list,
                ns: res.data.ns,
                xuanz:[],
                count: newCount,
                num: allNum
              })
              wx.showToast({
                title: '操作成功',
                duration: 2000
              });
            },
          })
        }
      })
    }
  },

  //改变选框状态
  change:function(e){
    var that=this
    //得到下标
    var index = e.currentTarget.dataset.index
    //得到选中状态
    var select = e.currentTarget.dataset.select
    var ids = e.currentTarget.dataset.id
    
    if(select == "circle"){
       var stype="success"
       console.log(ids)
       xx.push(ids);
       that.setData({
         xuanz: xx,
       })
    }else{
       var stype="circle"
       for (var i = 0; i < xx.length; i++) {
         if (xx[i] == ids) {
           xx.splice(i, 1);
         }
       }
       that.setData({
         xuanz: xx,
       })
    }
    //wx.setStorageSync('xxx', xx);
    //把新的值给新的数组
   var newList= that.data.list
   newList[index].select=stype
    //把新的数组传给前台
    that.setData({
      list:newList
    })
    //调用计算数目方法
    that.countNum()
    //计算金额
    that.count()
  },
  //加法
  addtion:function(e){
    var id = e.currentTarget.dataset.id;
    var that=this
    //得到下标
    var index = e.currentTarget.dataset.index
    //得到点击的值
    var num = e.currentTarget.dataset.num
    //默认99件最多
    if(num<100){
      num++
    }
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/cart/pluse',
      data: {
        carid: id,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
      },
    })
    //把新的值给新的数组
    var newList = that.data.list
    newList[index].num =num
   
    //把新的数组传给前台
    that.setData({
      list: newList
    })
    //调用计算数目方法
    that.countNum()
    //计算金额
    that.count()
  },
  //减法
subtraction:function(e){
  var that = this;
  var id = e.currentTarget.dataset.id;
  //得到下标
  var index = e.currentTarget.dataset.index
  //得到点击的值
  var num = e.currentTarget.dataset.num
  //把新的值给新的数组
  var newList = that.data.list
  //当1件时，点击移除
  if (num == 1) {
    newList.splice(index,1)
  }else{
    num--
    newList[index].num = num
  }
  wx.request({
    url: 'https://www.cakeyouxuan.com/small/cart/reduce',
    data: {
      carid: id,
    },
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"  //post
    },
    success: function (res) {
    },
  })
  //把新的数组传给前台
  that.setData({
    list: newList
  })
  //调用计算数目方法
  that.countNum()
  //计算金额
  that.count()
},
//全选
allSelect:function(e){
  var that=this
  //先判断现在选中没
  var allSelect = e.currentTarget.dataset.select
  var newList = that.data.list
  if(allSelect == "circle"){
    //先把数组遍历一遍，然后改掉select值
    for (var i = 0; i < newList.length; i++) {
      newList[i].select = "success"
      xx.push(newList[i].id);
    }
    that.setData({
      xuanz: xx,
    })
    var select="success"
  }else{
    for (var i = 0; i < newList.length; i++) {
      newList[i].select = "circle"
      xx = [];
    }
    that.setData({
      xuanz: xx,
    })
    var select = "circle"
  }
  that.setData({
    list:newList,
    allSelect:select
  })
  //调用计算数目方法
  that.countNum()
  //计算金额
  that.count()
},
//计算数量
countNum:function(){
  var that=this
  //遍历数组，把既选中的num加起来
  var newList = that.data.list
  var allNum=0
  for (var i = 0; i < newList.length; i++) {
        if(newList[i].select=="success"){
          allNum += parseInt(newList[i].num) 
        }
  }
  parseInt
  // console.log(allNum)
  that.setData({
    num:allNum
  })
},
//计算金额方法
count:function(){
  // var price;
  var that=this 
  //思路和上面一致
  //选中的订单，数量*价格加起来
  var newList = that.data.list
  
  var newCount=0
  for(var i=0;i<newList.length;i++){
    if(newList[i].select == "success"){
      newCount += newList[i].num * parseInt(newList[i].price.replace(/,/g, ''))
    }
  }
  // console.log('这里总价格');console.log(price);

  that.setData({
    count: newCount.toFixed(2)
  })
  
},

onLoad: function (options) {
  // 生命周期函数--监听页面加载 
  showView: (options.showView == "true" ? true : false)  
  var uid = app.whereId.uid; //uid
  console.log(uid);
  var that = this;
  var openid = app.globalData.openid; //openid
  var shopid = app.whereId.shopid;
  xx = [];
  wx.request({
    url: 'https://www.cakeyouxuan.com/small/cart/index',
    data: {
      uid: uid,
      shopid: shopid,
      tag: 1,
    },
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"  //post
    },
    success: function (res) {
      // console.log(res); console.log("12132");
      that.setData({
        list: res.data.list,
        allSelect: res.data.allSelect,
        num: res.data.numss,
        count: res.data.count,
        xuanz:xx,
      });
    },
  })
}, 

  onShow: function (options){
  var uid = app.whereId.uid; //openid
  var that = this;
  var openid = app.globalData.openid; //openid
  var shopid = app.whereId.shopid;
  xx = [];
  wx.request({
    url: 'https://www.cakeyouxuan.com/small/cart/index',
    data: {
      uid: uid,
      shopid: shopid,
      tag: 1,
    },
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"  //post
    },
    success: function (res) {
      console.log(res); console.log("12132");
      that.setData({
        list: res.data.list,
        allSelect: res.data.allSelect,
        num: res.data.numss,
        count: res.data.count,
      });
    },
  })
},


//点击变化后规格
onChangeShowState_2: function (e) {
  var that = this;
  var id = e.currentTarget.dataset.id;
  var price = e.currentTarget.dataset.name;
  var cid = e.currentTarget.dataset.cid;
  var xuhao = e.currentTarget.dataset.xuhao;
  wx.request({
    url: 'https://www.cakeyouxuan.com/small/cart/guiges',
    data: {
      id:id,
    },
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"  //post
    },
    success: function (res) {
      that.setData({
        listclass: res.data.listclass,
        price: price,
        danjia: res.data.danjia,
        ide: cid
      })
    },
  })
  that.setData({
    clickId:"",
    showView: (!that.data.showView),
    xuhao:xuhao,
  })
},

// 页面跳转结算
skip: function (e) {
  console.log(e);
  var that = this;
  var cid = e.currentTarget.dataset.name;
  var uid = app.whereId.uid; //uid
  var shopid = app.whereId.shopid;
  if (cid == "") {
    wx.showToast({
      title: '未选中购买对象',
      icon: 'fail',
      duration: 800
    })
  } else {

    wx.navigateTo({
      url: "/pages/yiguo/detail/detail?cid=" + cid + "&uid=" + uid + "&shopid=" + shopid
    })
  }
},



//点击完成
onChangeShowState_ok: function (e) {
  var that = this;
  var list_ = this.data.list;
  var index_ = e.currentTarget.dataset.ind;

  if (list_[index_].show == true) {
    for (let i = 0; i < list_.length; i++) {
      list_[index_].show = false;
      list_[index_].xs = 1;
    }
  } else {
    for (let i = 0; i < list_.length; i++) {
      list_[index_].show = true;
      list_[index_].xs = 0;
    }
  }
// 判断删除和完成的切换
  var ns = 0;
  for (let i = 0; i < list_.length; i++) {
    ns += list_[i].xs;
  }
  var list_len = list_.length;
  if (ns == list_len) {
    ns = 1;
  } else {
    ns = 0;
  }

  that.setData({
    ns: ns,
    list: list_,
    xuhao: index_,
  })
},


// 点击减号
    bindMinus: function () {
      // var num = e.currentTarget.dataset.num
      var num = this.data.num;
             // 如果大于1时，才可以减  
            if (num > 1) {
                 num --;
      }
         // 只有大于一件的时候，才能normal状态，否则disable状态  
      var minusStatus = num <= 1 ? 'disabled' : 'normal';
             // 将数值与状态写回  
           this.setData({
              num: num,
              minusStatus: minusStatus,
              // list: newList  
      });
  },
    /* 点击加号 */  
    bindPlus: function () {
           var num = this.data.num;
           // 不作过多考虑自增1  
          num ++;
          // 只有大于一件的时候，才能normal状态，否则disable状态  
          var minusStatus = num < 1 ? 'disabled' : 'normal';
          // 将数值与状态写回  
           this.setData({
              num: num,
              minusStatus: minusStatus  
        });
    }

})